﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Localiser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoadResx_Click(object sender, EventArgs e)
        {
            string translationFolder = Path.Combine(txtResxFile.Text, "Translations");
            string[] files = Directory.GetFiles(txtResxFile.Text, "frm*.resx", SearchOption.TopDirectoryOnly);

            ResxTranslator translator = new ResxTranslator(translationFolder, "iContactTexts");

            CultureInfo[] cultures;

            if (radioAllCultures.Checked)
            {
                cultures = translator.GetAvailableTranslations();
            }
            else
            {
                cultures = new CultureInfo[] { new CultureInfo(txtCultureName.Text) };
            }

            foreach (CultureInfo culture in cultures)
            {
                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    string ext = Path.GetExtension(fileName);
                    string strippedFileName = fileName.Remove(fileName.Length - ext.Length, ext.Length);
                    string destFile = Path.Combine(Path.GetDirectoryName(file),
                                                   string.Format("{0}.{1}.resx", strippedFileName, culture.Name));
                    translator.GenerateTranslatedResource(file, destFile, culture);
                }
            }
        }

        private void radioAllCultures_CheckedChanged(object sender, EventArgs e)
        {
            txtCultureName.Enabled = !radioAllCultures.Checked;
        }
    }
}
